package com.raising.framework.controller;

import com.raising.framework.id.IdUtils;
import com.raising.modules.sys.service.OrganizationService;
import com.raising.utils.LogUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 基础controller
 * @author GaoYuan
 */
public class BaseController {
    /**Log4j日志处理*/
    protected org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 管理基础路径
     */
    @Value("${adminPath}")
    protected String adminPath;

    /**
     * 前端基础路径
     */
    @Value("${frontPath}")
    protected String frontPath;

    /**
     * rest接口路径
     */
    @Value("${restPath}")
    protected String restPath;

    /**
     * 前端URL后缀
     */
    @Value("${urlSuffix}")
    protected String urlSuffix;

    /**
     * 获取ID
     * @author GaoYuan
     * @date 2018/6/15 上午11:16
     */
    protected String nextId(){
        return IdUtils.nextId();
    }

    /**
     * 客户端返回JSON字符串
     * @param response
     * @param object
     * @return String
     */
    protected String renderString(HttpServletResponse response, Object object) {
        JSONObject json = JSONObject.fromObject(object);
        return renderString(response, json.toString(),
                "application/json");
    }

    /**
     * 返回JSONP的数据
     * @param response
     * @param object
     * @param callback
     * @return
     */
    protected String renderString(HttpServletResponse response, Object object,
                                  String callback) {
        JSONObject json = JSONObject.fromObject(object);
        return renderString(response,
                callback + "(" + json.toString() + ")",
                "application/text");
    }

    /**
     * 客户端返回字符串
     * @param response
     * @param string
     * @return
     */
    protected String renderString(HttpServletResponse response, String string,
                                  String type) {
        try {
            response.reset();
            response.setContentType(type);
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 添加Model消息
     * @param messages
     */
    protected void addMessage(Model model, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        model.addAttribute("msg", sb.toString());
    }

    /**
     * 添加Flash消息
     * @param messages
     */
    protected void addMessage(RedirectAttributes redirectAttributes,
                              String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        redirectAttributes.addFlashAttribute("msg", sb.toString());
    }


    /*****操作日志*********/
    /**
     * 插入操作日志
     * @author GaoYuan
     * @datetime 2018/1/17 下午2:50
     */
    public static void olog(String content){
        LogUtils.saveOlog(content);
    }

    public static void olog(String content,String param){
        LogUtils.saveOlog(content,param);
    }

}
